
import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store/store'
import iView from 'iView'
import Portal from '@/components/HelloWorld'
import Login from '@/system/login'
import * as types from '@/store/types'

import Manager from '@/manager/index'
import ProjectContent from '@/manager/index/content'

import MyIndex from '@/manager/project/index'

import TodoContent from '@/manager/project/todo/content'

import Calendar from '@/manager/project/calendar/content'

import Files from '@/manager/project/files/content'

import Project from '@/manager/project/project/content'

import Share from '@/manager/project/share/content'

import Test from '@/manager/own-space/test'


Vue.use(Router)

export const page404 = {
  path: '/*',
  name: 'error-404',
  meta: {
    title: '404-页面不存在'
  },
  component: () => import('@/manager/error-page/404.vue')
};
export const routes = [
  {
    path: '/',
    name: 'Portal',
    redirect:'/manager',
    meta: {
      title: '首页'
    },
    component: Portal
  },
  {
    path: '/manager',
    name: '项目管理',
    redirect:'/manager/projectContent',
    component: Manager,
    children: [{
      path: 'projectContent',
      name: '项目',
      meta: {
        title: '任务',
        requireAuth: true
      },
      component: ProjectContent
    },{
      path: 'calendar',
      name: '日历',
      meta: {
        title: '任务',
        requireAuth: true
      },
      component: Calendar
    },{
      path: 'my',
      name: '个人中心',
      meta: {
        title: '个人中心',
        requireAuth: true
      },
      component: () =>  import('@/manager/my/index')
    },{
      path: 'setting',
      name: '设置',
      meta: {
        title: '设置',
        requireAuth: true
      },
      redirect:'/manager/setting/userinfo',
      component: () => import('@/manager/setting/index'),
      children: [{
        path: 'userinfo',
        name: '个人信息',
        meta: {
          title: '个人信息',
          requireAuth: true
        },
        component: () => import('@/manager/own-space/own-infor')
      }]
    },{
          path: 'project',
            name: '项目',
            meta: {
            title: '任务',
              requireAuth: true
          },
          component: MyIndex,
            redirect:'/manager/project/project',
            children: [
            {
              path: 'project',
              name: '项目',
              meta: {
                title: '项目',
                requireAuth: true
              },
              component: Project

            },{
              path: 'todo',
              name: '待办',
              meta: {
                title: '待办',
                requireAuth: true
              },
              component: TodoContent,
            },{
              path: 'files',
              name: '文件',
              meta: {
                title: '文件',
                requireAuth: true
              },
              component: Files

            },{
              path: 'share',
              name: '分享',
              meta: {
                title: '分享',
                requireAuth: true
              },
              component: Share

            },{
              path: 'tongji',
              name: '统计',
              meta: {
                title: '统计',
                requireAuth: true
              },
              component: () => import('@/manager/project/tongji/projectInfo')
        }
      ]
    }]
  },{
  path: 'calendar',
    name: '日历',
    meta: {
    title: '任务',
      requireAuth: true
  },
  component: Calendar

},{
  path: '/system',
    name: '',
    component: Login,
    children: [{
    path: 'login',
    name: 'Login',
    meta: {
      title: '登录页'
    },
    component: Login
  }]
} ,page404
]
const router = new Router({
  routes,
  history: true
})
router.beforeEach((to, from, next) => {
  iView.LoadingBar.start()
window.document.title = to.meta.title

if (to.matched.some(r => r.meta.requireAuth)){
  if (store.state.token) {
    next()
  } else {
    next({
      path: '/system/login',
      query: {redirect: to.fullPath}
    })
  }
}else {
  next()
}
})

router.afterEach(route => {
  iView.LoadingBar.finish()
})
export default router
